<template>
  <div class="list-table" style="box-shadow: none;">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <!-- <el-form-item :label="$t('发票号码')" prop="invoiceCode">
        <el-input
          v-model="queryParams.invoiceCode"
          :placeholder="$t('发票号码')"
          @blur="invoiceList = parentData"
        />
      </el-form-item> -->
      <el-form-item :label="$t('说明')" prop="remark">
        <el-input
          v-model="queryParams.remark"
          :placeholder="$t('说明')"
          @blur="invoiceList = parentData"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="invoiceList"
      border
      style="height: 85%;overflow: auto;"
      @select-all="multiOperation"
      @select="multiOperation"
    >
      <el-table-column type="selection" width="40" prop="disabled" :selectable="selectable" show-overflow-tooltip />
      <el-table-column :label="$t('发票')" align="center" prop="fileId">
        <template slot-scope="scope">
          <span
            v-if="scope.row.fileList !== undefined && scope.row.fileList !== null"
            class="link-type"
            style="width: 100%;float: left;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;"
            @click="onlinePreview(scope.row.fileList)"
          >
            {{ scope.row.fileList.name }}
          </span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('票面金额(元)')" align="center" prop="money" width="150">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.money, '') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('发票时间')" align="center" prop="time" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('发票号码')" align="center" prop="invoiceCode" />
      <el-table-column :label="$t('说明')" align="center" prop="remark" />
      <el-table-column :label="$t('分摊明细')" align="center" width="80">
        <template slot-scope="scope">
          {{ formatIndex(scope.row.index) }}
        </template>
      </el-table-column>
    </el-table>
    <!-- 文件预览 -->
    <OnlinePreview ref="OnlinePreview" />

    <div slot="footer" style="text-align: right;margin-top:20px;">
      <span style="float: left;">{{ $t('数量:') }} {{ invoiceList.length }}</span>
      <el-button type="success" :disabled="multiSelectButton" size="mini" @click="choiceUseInvoice">{{ $t('确定') }}</el-button>
    </div>
  </div>
</template>

<script>
export default {
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 发票夹表格数据
      invoiceList: [{
        money: undefined,
        time: undefined,
        invoiceCode: undefined,
        remark: undefined,
        fileList: { id: undefined, name: ' ' }
      }],
      // 查询参数
      queryParams: {
        invoiceCode: undefined,
        remark: undefined
      },
      choiceUseInvoiceArray: [],
      disabledStatus: true,
      // 多选
      multiSelectButton: true,
      // 父页面传来的 发票明细数据组
      parentData: [],
      // 父组件分摊明细的下标
      oaExpenseInvoiceIndex: 0
    }
  },
  watch: {
  },
  mounted() {
  },
  created() {
  },
  methods: {
    /** 查询发票夹列表 */
    getList(data, index) {
      console.log('展示工具数据')
      this.invoiceList = data
      this.parentData = data
      this.oaExpenseInvoiceIndex = index
      this.$nextTick(() => {
        this.invoiceList.forEach((item, i) => {
          // 当if为true时 将当前分摊明细所选的发票回显时选中. 相反时, 已经被其他明细使用. 剩下的就是没参与计算的 -- 是否禁用交给selectable处理
          if (this.oaExpenseInvoiceIndex === item.index || item.index !== undefined) {
            // 当前分摊明细选中的发票
            this.$refs.multipleTable.toggleRowSelection(this.invoiceList[i], true)
          } else {
            this.$refs.multipleTable.toggleRowSelection(this.invoiceList[i], false)
          }
        })
      })
    },
    /** 搜索按钮操作 */
    queryHandle() {
      if (this.queryParams.invoiceCode === undefined && this.queryParams.remark === undefined) {
        return
      }
      this.invoiceList = []
      this.parentData.forEach(item => {
        if (item.remark !== null && item.remark !== undefined && item.remark.trim() !== '') {
          if (item.remark.includes(this.queryParams.remark)) {
            this.invoiceList.push(item)
          }
        }
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.getList()
    },
    // 确定按钮
    choiceUseInvoice() {
      this.loading = true
      setTimeout(() => {
        if (this.disabledStatus) {
          this.disabledStatus = false
          // 整理数据 只把选中的(不包含禁用)的传递过去
          console.log('整理选中的发票')
          var tempSendArr = []
          this.choiceUseInvoiceArray.forEach(item => {
            if (item.index === this.oaExpenseInvoiceIndex || item.index === undefined) {
              tempSendArr.push(item)
            }
          })
          this.choiceUseInvoiceArray = tempSendArr
          this.choiceUseInvoiceSend()
        }
        this.loading = false
      }, 500)
    },
    // 多选禁用
    selectable(row, index) {
      console.log(row, index)
      if (row.index !== undefined && row.index !== this.oaExpenseInvoiceIndex) {
        return false
      } else {
        return true
      }
    },
    // 多选操作
    multiOperation(selection) {
      const choiceCount = selection.length
      // 选中的大于0 展示批量下载
      if (choiceCount > 0) {
        this.multiSelectButton = false
        this.choiceUseInvoiceArray = []
        selection.forEach(item => {
          this.choiceUseInvoiceArray.push(item)
        })
      } else {
        this.multiSelectButton = true
      }
    },
    // 批量使用
    choiceUseInvoiceSend() {
      console.log('数据传递父组件')
      if (this.choiceUseInvoiceArray !== undefined && this.choiceUseInvoiceArray !== null) {
        this.$emit('addApportionmentCalculation', this.choiceUseInvoiceArray, this.oaExpenseInvoiceIndex)
        this.$parent.$parent.oaExpenseDistributeInvoiceLoading = false
      } else {
        this.msgError('先选择!')
        this.loading = false
      }
    },
    // 预览附件
    onlinePreview(row) {
      this.$refs.OnlinePreview.onlinePreviewFile(row.id)
    },
    formatIndex(index) {
      if (index === undefined) {
        return ''
      } else {
        index = parseInt(index) + 1
        if (isNaN(index)) {
          return ''
        } else {
          return index
        }
      }
    }
  }
}
</script>
